package com.qlm.controller;



import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.upload.UploadFile;
import com.qlm.entity.Img;
import com.qlm.interceptor.AdminOperateInterceptor;
import com.qlm.interceptor.AuthInterceptor;
import com.qlm.service.IImgService;
import com.qlm.service.impl.ImgServiceImpl;
import com.qlm.tools.QiniuUtil;
import com.qlm.view.ResultMessage;

@Before({AuthInterceptor.class , AdminOperateInterceptor.class})
public class ImgManageController extends Controller {
	protected final static Logger logger = LoggerFactory
			.getLogger(GiftManageController.class);
	private IImgService imgService = new ImgServiceImpl();
	
	public void toImgManagerPage(){
		String id = getPara("id","-1");
		List<Img> list = imgService.getAllImg(id);
		//传递参数
		setAttr("gift_id",id);
		setAttr("imgList", list);
		renderJsp("/page/imgManage.jsp");
	}
	
	/**
	 * 保存图片
	 */
	public void saveImg(){
		UploadFile file = getFile("file");
		String gift_id = getPara("gift_id","-1");
		ResultMessage rm = imgService.saveImg(gift_id,file);
		renderJson(rm);
	}
	
	/**
	 * 删除图片
	 */
	public void delImg(){
		Integer id = getParaToInt("id");
		logger.info("method delImg para id=("+id+")");
		ResultMessage rm = imgService.delImg(id);
		logger.info("method delImg result rm=("+rm+")");
		renderJson(rm);
	}
	
	/**
	 * 保存URL链接
	 */
	public void saveUrl(){
		String id_ = getPara("id");
		String url_ = getPara("url");		
		ResultMessage rm = imgService.saveUrl(id_, url_);
		logger.info("method saveUrl result rm=("+rm+")");
		renderJson(rm);
	}
}
